<!DOCTYPE html>
<html lang="en">
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
    <style>
		.heading {
			background: #1D9FDF;
			height: 130px;
			width: 100%;
			padding-top: 10px;
			padding-bottom: 10px;
			color: white;
			text-align: center;
		}

		#heading {
			text-align: center;
		}

		.body {

			width: 100%;
		}
		table {
			border-collapse: collapse;
			width: 100%;
		}

		td {
			border: 1px solid black;
			height:30px;
			width:280px;
			text-align:center;
			font-weight: bold;
		}
		 .reporttable {
            border-collapse: collapse;
        }
      
    </style>



    <script>	

	 $("#export").click(function (e) {
                e.preventDefault();

                //getting data from our table
                var data_type = 'data:application/vnd.ms-excel';
                var table_div = document.getElementById('report_table');
                var table_html = table_div.outerHTML.replace(/ /g, '%20');

                var a = document.createElement('a');
                a.href = data_type + ', ' + table_html;
                a.download = 'RP Report.xls';
                a.click();
            });

	var json = "";
        var district = "", sub = "";
        var checkOuLevel = function (ou) {
            $.ajax({
                async: false,
                type: "GET",
                url: "../api/organisationUnits/" + ou + ".json",
                success: function (data) {
                    json = data;

                }
            });
            return json;
        };
		

        var month;
        var getMonth = function (a) {
            if (a == "1" || a == "01") { month = "January" }
            if (a == "2" || a == "02") { month = "February" }
            if (a == "3" || a == "03") { month = "March" }
            if (a == "4" || a == "04") { month = "April" }
            if (a == "5" || a == "05") { month = "May" }
            if (a == "6" || a == "06") { month = "June" }
            if (a == "7" || a == "07") { month = "July" }
            if (a == "8" || a == "08") { month = "August" }
            if (a == "9" || a == "09") { month = "September" }
            if (a == "10") { month = "october" }
            if (a == "11") { month = "November" }
            if (a == "12") { month = "December" }
        };

        var getFirstThreeColumn = function (ou) {
                $.ajax({
                    async: false,
                    type: "GET",
                    url: "../api/organisationUnits/" + ou + ".json?fields=ancestors[id,name,level],displayName",
                    success: function (data) {
                        for(var i=0;i<data.ancestors.length;i++){
                            if(data.ancestors[i].level == 2){
                                district = data.ancestors[i].name; 
								sub = data.displayName; 
                            }
                                                                                      
                        }

                    }
                });
                return district,sub;
            };
			
			 $(document).ready(function () {
            var url = window.location.href;
            var pe1 = url.split("&")[1];
            var pe = pe1.split("=")[1];
            var monthCode = pe.substr(4, 6);
            getMonth(monthCode);
            var year = pe.substr(0, 4);
            var ou1 = url.split("&")[2];
            var ou = ou1.split("=")[1];
            var ouLeveldata = checkOuLevel(ou);
            var ouLevel = ouLeveldata.level;
            var index = 1;
			
            var s1col1 = "", s1col2 = "", s1col3 = "", s1col4 = "", s1col5 = "", s1col6 = "", s1col7 = "", s1col8 = "", s1col9 = "", s1col10 = "";
            var s1col11 = "", s1col12 = "", s1col13 = "", s1col14 = "", s1col15 = "", s1col16 = "", s1col17 = "", s1col18 = "", s1col19 = "";
			var maletotal = "", femaletotal = "", othertotal = "", finaltotal = "";
			
            var printData = function (ou) {
                var  json2 = "";
				
				//stage 1 api
                $.ajax({
                    async: false,
                    type: "GET",
                    url: "../api/26/analytics/events/query/Mtjl5oc1bDZ.json?dimension=pe:"+pe+"&dimension=ou:"+ou+"&dimension=HXOiWn8VQ5T&dimension=XXdx7ZDH59h&dimension=Go5FALp7rZM&dimension=XeWg8e7eJzC&dimension=UMKyt5PlozR&dimension=QzxMPMx1P4G&dimension=Jyv1Nwm3viJ&dimension=uj2a0UxeNkJ&dimension=MGB7Pd9fff6&dimension=QT6nLEF8SrP&dimension=tr3do2Rt8Xw&dimension=v7mGy9DFfXd&dimension=sHJP6sfPMtb&dimension=e4gIjnqjHH0&dimension=ICctYmyTVSO&dimension=jrEQhjhktcG&dimension=ueB1GzRCpDR&dimension=YUcO16rZT65&dimension=IQQCkZoYroC&dimension=Xq6jDla5y8f&dimension=mIZmK7J17mJ&dimension=Bsn9nWtIOsA&dimension=z85ytgKVH4q&dimension=TsrxZqL4FPs&dimension=lK9aNNpT7Yr&dimension=pw2CJfncRrz&dimension=fAcxKXwMZ17&dimension=X3Uh7lXXc2D&dimension=wv7kbU1jsGn&dimension=YKKc5eO6QP7&stage=OPcR3xIKKkK&displayProperty=SHORTNAME&outputType=EVENT&pageSize=100&skipPaging=true",
                    success: function (data) {
                        json2 = data;
                        for (var j = 0; j < data.headers.length; j++) {

                            if (data.headers[j].column == "Training_Name") {
                                s1col1 = j;
                            }
                            if (data.headers[j].column == "Training_Start date") {
                                s1col2 = j;
                            }
                            if (data.headers[j].column == "Training_End date") {
                                s1col3 = j;
                            }
                            if (data.headers[j].column == "Trainings_Participants (Dalit,Male)") {
                                s1col4 = j;
                            }
                            if (data.headers[j].column == "Trainings_Participants (Dalit,Female)") {
                                s1col5 = j;
                            }
                            if (data.headers[j].column == "Trainings_Participants (Dalit,Others)") {
                                s1col6 = j;
                            }
                            if (data.headers[j].column == "Dalit Total") {
                                s1col7 = j;
                            }
                            if (data.headers[j].column == "Trainings_Participants (Janajati,Male)") {
                                s1col8 = j;
                            }
                            if (data.headers[j].column == "Trainings_Participants (Janajati,Female)") {
                                s1col9 = j;
                            }
                            if (data.headers[j].column == "Trainings_Participants (Janajati,Others)") {
                                s1col10 = j;
                            }
                            if (data.headers[j].column == "Janajati Total") {
                                s1col11 = j;
                            }
                            if (data.headers[j].column == "Trainings_Participants (Others,Male)") {
                                s1col12 = j;
                            }
                            if (data.headers[j].column == "Trainings_Participants (Others,Female)") {
                                s1col13 = j;
                            }
                            if (data.headers[j].column == "Trainings_Participants (Others,Others)") {
                                s1col14 = j;
                            }
                            if (data.headers[j].column == "Others Total") {
                                s1col15 = j;
                            }
                            if (data.headers[j].column == "Trainings_Participants (Contractors)") {
                                s1col16 = j;
                            }
                            if (data.headers[j].column == "Trainings_Participants (Local communities)") {
                                s1col17 = j;
                            }
                            if (data.headers[j].column == "Training_Participants (Field personnel)") {
                                s1col18 = j;
                            }
                            if (data.headers[j].column == "Training_Remark") {
                                s1col19 = j;
                            }
                        }
                    }
                });
				

				
				// first three columns
               getFirstThreeColumn(ou);
                for (var p = 0; p < json2.rows.length; p++) {
				maletotal = parseInt(json2.rows[p][s1col4]) + parseInt(json2.rows[p][s1col8]) + parseInt(json2.rows[p][s1col12]);
				femaletotal = parseInt(json2.rows[p][s1col5]) + parseInt(json2.rows[p][s1col9]) + parseInt(json2.rows[p][s1col13]);
				othertotal = parseInt(json2.rows[p][s1col6]) + parseInt(json2.rows[p][s1col10]) + parseInt(json2.rows[p][s1col14]);
				finaltotal = parseInt(json2.rows[p][s1col7]) + parseInt(json2.rows[p][s1col11]) + parseInt(json2.rows[p][s1col15]);
                    var row = "<tr>";
                    row = row + "<td>" + district + "</td><td>" + sub + "</td><td>" + json2.rows[p][s1col1] + "</td><td>" + json2.rows[p][s1col2] + "</td><td>" + json2.rows[p][s1col3] + "</td><td>" + json2.rows[p][s1col4] + "</td><td>" + json2.rows[p][s1col5] + "</td><td>" + json2.rows[p][s1col6] + "</td><td>" + json2.rows[p][s1col7] + "</td>";
                    row = row + "<td>" + json2.rows[p][s1col8] + "</td><td>" + json2.rows[p][s1col9] + "</td><td>" + json2.rows[p][s1col10] + "</td><td>" + json2.rows[p][s1col11] + "</td><td>" + json2.rows[p][s1col12] + "</td><td>" + json2.rows[p][s1col13] + "</td><td>" + json2.rows[p][s1col14] + "</td><td>" + json2.rows[p][s1col15] + "</td><td>"+maletotal+"</td><td>"+femaletotal+"</td><td>"+othertotal+"</td><td>"+finaltotal+"</td><td>" + json2.rows[p][s1col16] + "</td><td>" + json2.rows[p][s1col17] + "</td>";
                    row = row + "<td>" + json2.rows[p][s1col18] + "</td><td>" + json2.rows[p][s1col19] + "</td>";
                    row = row + "</tr>";
                    $('.reporttable').append(row);
                }
				};
				
			if (ouLevel == 1) {
                for (var p = 0; p < ouLeveldata.children.length; p++) {
                    var tempOu = ouLeveldata.children[p].id;
                    var tempOuData = checkOuLevel(tempOu);
                    if (tempOuData.children.length !== 0) {
                        for (var l = 0; l < tempOuData.children.length; l++) {
                            var tempOu2 = tempOuData.children[l].id;
                            //   var tempOuData2 = checkOuLevel(tempOu2);
							
                          //  var returnValue = checkOuLevel(tempOu2);
                            
                                printData(tempOu2, index);
                                index++;
                            
                        }
                    }
                }
            }
            else if (ouLevel == 2) {
                for (var p = 0; p < ouLeveldata.children.length; p++) {
                    var tempOu = ouLeveldata.children[p].id;
                    //var returnValue = checkOuLevel(tempOu);
                    
                        printData(tempOu, index);
                        index++;
                    
                }
            }
            else if (ouLevel == 3) {
                printData(ou, 1);
            }
            else {
                alert("Wrong selection");
                window.history.back();;
            }
        });

    </script>
</head>

<body>
<div class="outer">
	<div class="heading">
		<div style="float:left;height:100%;width:20%">
			<img src="../dhis-web-commons/security/DRILP_Logo.jpg" width="80%" height="80%">
		</div>
		<div id="heading" style="float:left;width:60%;height:100%">
			<p style="font-size:16px;">Earthquake Emergency Assistance Project (EEAP)</p>
			<p style="font-size:20px;">Rehabilitation and Reconstruction of District Roads</p>
			<p style="font-size:16px;">Training/Orientation/Awareness Raising Event Recods</p>
		</div>
		<div style="float:left;height:100%;width:20%"></div>
	</div>

</div>
<div>


    
</div>
<div id="mydiv">
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<center><button id="export">Export to Excel</button></center>
        <br>
    <table class="table table-bordered reporttable" id="report_table" width="100%">
        <tr>
			<td rowspan="3">District</td>
             <td rowspan="3">Sub-project</td>
			 <td rowspan="3" id="YKKc5eO6QP7">Name of the event</td>
            <td rowspan="3">From Date</td>
            <td rowspan="3">To Date</td>
			<td colspan="12">Participants</td>
			<td colspan="4" rowspan="2">Total Participants</td>
			<td colspan="3" rowspan="2">Represented By</td>
			<td rowspan="3"> Remarks</td>
			
			
        </tr>
		
		<tr>

		<td colspan="4">Dalit</td>
		<td colspan="4">Janajati</td>
		<td colspan="4">Others</td>

		
		</tr>
		
		<tr id="cellid">
			<td id="male1">Male</td>
			<td id="female1">Female</td>
			<td id="other1">Others</td>
			<td id="total1">Total</td>
			<td id="male2">Male</td>
			<td id="female2">Female</td>
			<td id="other2">Others</td>
			<td id="total2">Total</td>
			<td id="male3">Male</td>
			<td id="female3">Female</td>
			<td id="other3">Others</td>
			<td id="total3">Total</td>
			<td id="maletotal">Male</td>
			<td id="femaletotal">Female</td>
			<td id="othertotal">Others</td>
			<td id="finaltotal">Total</td>
			<td id="">Contractor</td>
			<td id="">Local Communiies</td>
			<td id="">Field Personnel</td>
			</tr>



    </table>


</div>
</body>

</html>